package com.lfk.justwetools.Component.CartoonView;

import com.lfk.justwetools.Utils.Log;
import ohos.agp.utils.Point;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.agp.components.AttrSet;
import ohos.agp.components.ListContainer;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author liufengkai
 *         Created by liufengkai on 16/2/22.
 */
public class CartoonView extends ListContainer {
    private Context mContext;
    private String dirPath;
    private CartoonAdapter adapter;
    private int mScreenHeight;

    public CartoonView(Context context, AttrSet attrs) {
        super(context, attrs);
        this.mContext = context;
        attrs.getAttr("dir_path").ifPresent(attr-> this.dirPath =
                context.getExternalFilesDir(null) + attr.getStringValue());
        init(context);
    }

    public CartoonView(Context context, String dirPath) {
        super(context);
        this.mContext = context;
        this.dirPath = dirPath;
        init(context);
    }

    private void init(Context context){
        Point screenSize = new Point();
        DisplayManager.getInstance().getDefaultDisplay(context).get().getSize(screenSize);
        mScreenHeight = screenSize.getPointYToInt();
    }

    public void start() {
        this.adapter = new CartoonAdapter(mContext, getFileList());
        this.setItemProvider(adapter);
    }

    private ArrayList<String> getFileList() {
        ArrayList<String> fileList = new ArrayList<>();

        File[] list = new File(dirPath).listFiles();

        if (list != null) {
            for (int i = 0; i < list.length; i++) {
                Log.e("lfk", list[i].getName());
                if (list[i].getName().endsWith(".jpg")
                        || list[i].getName().endsWith(".png"))
                    fileList.add(list[i].getPath());
            }

            Arrays.sort(list);
        }
        return fileList;
    }
}
